package com.sduonline.isdapp.servlet;

import java.io.IOException;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sduonline.isdapp.jdbc.ArticleDao;
import com.sduonline.isdapp.model.temp.ArticleDetail;
import com.sduonline.isdapp.util.TimeUtil;

/**
 * Servlet implementation class ArticleServlet
 */
@WebServlet("/ArticleServlet")
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ArticleServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String article_id = request.getParameter("articleId");
		System.out.println("article_id:" + article_id);
		try {
			int articleId = Integer.parseInt(article_id);
			ArticleDao adao = new ArticleDao();
			ArticleDetail ad = adao.getArticleDetail(articleId);
			// 如果文章不存在，做处理
			if (ad == null) {
				ad = new ArticleDetail();
				ad.setId(articleId);
				ad.setContent("<strong>您要找的文章不存在...</strong>");
			} else {
				// 若 != null
				System.out.println(ad.getTime());
				if (ad.getTime() != null && !"".equals(ad.getTime())) {
					String time = TimeUtil.convertTo_yyyyMMdd(ad.getTime() + "000");
					ad.setTime(time);
				} else {
					ad.setTime("");
				}
			}
			request.setAttribute("articleDetail", ad);
			request.getRequestDispatcher("/article.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			response.sendRedirect("article404.html");
		} catch (ParseException e) {
			e.printStackTrace();
			response.sendRedirect("article404.html");
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

	}

	@Override
	public void init() {
		System.out.println("init start...");
	}

	@Override
	public void destroy() {
		System.out.println("init stop...");
	}
}
